package com.patterns.proxy.staticproxy;

import java.util.Random;

/**
 * @author fwj
 * @date 2021/4/22 10:34
 **/
public class Tank implements Movable {

    /**
     * 模拟坦克移动了一段时间
     */
    @Override
    public void move() {
        System.out.println("Tank moving claclaclacla...");
        try {
            Thread.sleep(new Random().nextInt(10000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new TankTimeProxy(new TankLogProxy(new Tank())).move();
    }
}

//时间代理
class TankTimeProxy implements Movable {
    //被代理
    Movable movable;

    public TankTimeProxy(Movable movable) {
        this.movable = movable;
    }

    @Override
    public void move() {
        long start = System.currentTimeMillis();
        movable.move();
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}

//日志代理
class TankLogProxy implements Movable {
    //被代理
    Movable movable;

    public TankLogProxy(Movable movable) {
        this.movable = movable;
    }

    @Override
    public void move() {
        System.out.println("start moving ...");
        movable.move();
        System.out.println("end moving ...");
    }
}

interface Movable {
    void move();
}